<#import "template.ftl" as layout>
<@layout.registrationLayout; section>
    <#if section = "title">
        ${msg("loginTitle",realm.name)}
    <#elseif section = "header">
        设置手机号码
    <#elseif section = "form">
        <form id="kc-totp-login-form" class="${properties.kcFormClass!}" action="${url.loginAction}" method="post">
            <div class="${properties.kcFormGroupClass!}">
                <div class="${properties.kcLabelWrapperClass!}">
                    <label for="phoneNumber" class="${properties.kcLabelClass!}">输入手机号码</label>
                </div>

                <div class="${properties.kcInputWrapperClass!}">
                    <input id="phoneNumber" name="phoneNumber" type="text" class="${properties.kcInputClass!}"
                           value="${phoneNumber!''}" />
                </div>
            </div>

            <div class="${properties.kcFormGroupClass!}">
                <div class="${properties.kcLabelWrapperClass!}">
                    <label for="validateCode" class="${properties.kcLabelClass!}">输入验证码</label>
                </div>
                <div class="${properties.kcInputWrapperClass!}">
                    <input id="validateCode" name="validateCode" type="text" class="${properties.kcInputClass!}" />
                    <input class="${properties.kcButtonClass!} ${properties.kcButtonPrimaryClass!} ${properties.kcButtonBlockClass!} ${properties.kcButtonLargeClass!}"
                           type="button" value="发送验证码" id="sendBtn" style="width: 100px;margin: 3px auto auto;"/>
                </div>
            </div>


            <div class="${properties.kcFormGroupClass!}">
                <div id="kc-form-options" class="${properties.kcFormOptionsClass!}">
                    <div class="${properties.kcFormOptionsWrapperClass!}">
                    </div>
                </div>

                <div id="kc-form-buttons" class="${properties.kcFormButtonsClass!}">
                    <div class="${properties.kcFormButtonsWrapperClass!}">
                        <input class="${properties.kcButtonClass!} ${properties.kcButtonPrimaryClass!} ${properties.kcButtonLargeClass!}" name="login" id="kc-login" type="submit" value="提交"/>
                    </div>
                </div>
            </div>
            <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
            <script type="text/javascript">
                $(function () {
                    $("#sendBtn").on("click", function () {
                        var phone = $("#phoneNumber").val();
                        if (!phone) {
                            alert("手机不能为空");
                            return;
                        }
                        $.ajax({
                            url: window.location.origin + "/realms/${realm.name}/sms-rest/sendMfaSmsCode?phoneNumber=" + phone,
                            contentType: "application/json",
                            data: phone,
                            type: "POST",
                            success: function (data) {
                                console.log(data.code);
                                if (data.code === 1) {
                                    alert(data.msg);
                                }
                            }, error: function (data) {
                                console.log(data);
                                alert("验证码发送失败");
                            }
                        })
                    })
                })
            </script>
        </form>
    </#if>

</@layout.registrationLayout>